package cn.neo.support.layer;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageButton;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import cn.neo.support.R;
import cn.neo.support.utils.ScreenUtil;


/**
 * <b>Name:</b>输入框Layer<br>
 * <b>Author:</b> WK（wangkun@xjzhicheng.com)<br>
 * <b>Date:</b> 2017-02-06<br>
 * <b>Describe:</b> 自定义输入框的层，带遮罩和相关逻辑<br>
 * <p>title<br>
 * <ol>
 * <li>write something content..</li>
 * </ol>
 * </p>
 * <b>Version:</b>V1.0<br>
 * <b>Revision History:</b><br>
 */
public class InputLayer extends RelativeLayout {

    protected int mInputLayoutID;
    protected int mInputDividerID;
    protected LinearLayout mBgLayout;//背景遮罩层
    protected AppCompatEditText mEtInput;//输入框
    protected AppCompatImageButton mBtnSend;

    public interface OnEventListener {
        void onEvent(View v, EditText input);
    }

    public InputLayer(Context context) {
        this(context, null);
    }

    public InputLayer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputLayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(context, attrs);
        initView();
    }

    /**
     * 初始化View
     */
    private void initAttr(Context context, AttributeSet attrs) {
        // 设置样式属性
        if (attrs == null) return;
        TypedArray styleType = context.obtainStyledAttributes(attrs, R.styleable.InputLayer);
        mInputLayoutID = styleType.getResourceId(R.styleable.InputLayer_input_layout, 0);
        mInputDividerID = styleType.getResourceId(R.styleable.InputLayer_input_divider, 0);
        styleType.recycle();
    }

    private void initView() {
        //输入框
        LayoutInflater.from(getContext()).inflate(mInputLayoutID, this, true);
        //分割线
        LinearLayout dividerRoot = new LinearLayout(getContext());
        dividerRoot.setId(R.id.input_layer_divider);
        RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rlp2.addRule(RelativeLayout.ABOVE, mInputLayoutID);
        rlp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        this.addView(dividerRoot, rlp2);
        LayoutInflater.from(getContext()).inflate(mInputDividerID, dividerRoot, true);
        //遮罩
        mBgLayout = new LinearLayout(getContext());
        mBgLayout.setId(R.id.input_layer_bg);
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        rlp.addRule(RelativeLayout.ABOVE, R.id.input_layer_divider);
        this.addView(mBgLayout, rlp);
        //Find View
        mEtInput = (AppCompatEditText) findViewById(R.id.input_layer_in);
        mBtnSend = (AppCompatImageButton) findViewById(R.id.input_layer_send);
        //listener
        setListener();
    }

    private void setListener() {
        mEtInput.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mBgLayout.setVisibility(VISIBLE);
                    mBgLayout.setClickable(true);
                    mBgLayout.setBackgroundColor(
                            ContextCompat.getColor(getContext(), R.color.black_opacity_20));
                } else {
                    mBgLayout.setVisibility(GONE);
                    mBgLayout.setClickable(false);
                    mBgLayout.setBackgroundColor(
                            ContextCompat.getColor(getContext(), android.R.color.transparent));
                }
            }
        });
    }

    /**
     * 发送点击事件
     */
    public void setSendListener(final OnEventListener onSendListener) {
        mBtnSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onSendListener.onEvent(v, mEtInput);
                hideInput();
            }
        });
    }

    /**
     * 取消输入的点击事件
     */
    public void setCancelListener(final OnEventListener onCancelListener) {
        mBgLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onCancelListener.onEvent(v, mEtInput);
                hideInput();
            }
        });
    }

    public void setHint(String hint) {
        mEtInput.setText("");
        mEtInput.setHint(hint);
    }

    public void showInput(String hint) {
        if (!TextUtils.isEmpty(hint)) {
            setHint(hint);
        }
        //输入框得到焦点
        mEtInput.requestFocus();
        //显示输入法
        ScreenUtil.showSoftKeyboard(getContext(), mEtInput);
    }

    public void finishSend() {
        mEtInput.setText("");
    }

    public void hideInput() {
        //输入框失去焦点
        mEtInput.clearFocus();
        //隐藏输入法
        ScreenUtil.hideSoftKeyboard(getContext(), mEtInput);
    }


}
